package com.api.user.service.impl;

import com.api.common.mybatis.ResultMap;
import com.api.user.dao.CommodityOrderMapper;
import com.api.user.model.Commodity;
import com.api.user.model.CommodityOrder;
import com.api.user.model.CommoditySpecs;
import com.api.user.model.OrderCommditys;
import com.api.user.service.CommodityOrderService;
import com.api.core.service.AbstractService;
import com.api.user.service.CommodityService;
import com.api.user.service.CommoditySpecsService;
import com.api.user.service.OrderCommditysService;
import com.api.user.vo.CommodityOrderVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.persistence.Entity;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import com.api.common.JSONUtils;
import com.api.core.response.Result;
import com.api.core.response.ResultGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * Created by wanghuiwen on 2021/04/17.
 */
@Service
@Transactional
public class CommodityOrderServiceImpl extends AbstractService<CommodityOrder> implements CommodityOrderService {
    @Resource
    private CommodityOrderMapper commodityOrderMapper;

    @Resource
    private CommoditySpecsService commoditySpecsService;

    @Resource
    private OrderCommditysService orderCommditysService;

    @Resource
    private CommodityService commodityService;

    @Override
    public Result list(String search, String order, Integer page, Integer size){
        Map<String, Object> params = JSONUtils.json2map(search);
        Map<String, Object> orderParams = JSONUtils.json2map(order);
        for (String key : orderParams.keySet()) {
                if (orderParams.get(key) != null && orderParams.get(key).equals("ascending")) orderParams.put(key, "asc");
                if (orderParams.get(key) != null && orderParams.get(key).equals("descending")) orderParams.put(key, "desc");
            }
        PageHelper.startPage(page, size);
        List<Map<String, Object>> res = commodityOrderMapper.baseList(params, orderParams);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(res);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @Override
    public Result orderUserList(Map<String, Object> params, String order, Integer page, Integer size) {
        Map<String, Object> orderParams = JSONUtils.json2map(order);
        for (String key : orderParams.keySet()) {
            if (orderParams.get(key) != null && orderParams.get(key).equals("ascending")) orderParams.put(key, "asc");
            if (orderParams.get(key) != null && orderParams.get(key).equals("descending")) orderParams.put(key, "desc");
        }
        orderParams.put("order_id","desc");
        PageHelper.startPage(page, size);
        List<CommodityOrderVo> res = commodityOrderMapper.orderUserList(params, orderParams);

        Map<String,Object> statistics = commodityOrderMapper.statistics(params);
        Map<String,Object> statisticsState = commodityOrderMapper.statisticsState(params);
        PageInfo<CommodityOrderVo> pageInfo = new PageInfo<>(res);
        Map<String,Object> result = new HashMap<>();
        result.put("list",pageInfo);
        result.put("statistics",statistics);
        result.put("statisticsState",statisticsState);

        return ResultGenerator.genSuccessResult(result);
    }

    @Override
    public Result completed(Long orderId) {
        CommodityOrder order = findById(orderId);
        order.setOrderState(3);
        update(order);
        return ResultGenerator.genSuccessResult();
    }

    @Override
    public  List<ResultMap<String,Object>> analysis(Integer type) {
        return commodityOrderMapper.analysis(type);
    }

    @Override
    public List<ResultMap<String, Object>> dayAnalysis() {

        return commodityOrderMapper.dayAnalysis();
    }
}
